package com.sinoma.G2project.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.*;
import java.util.stream.Collectors;

@Slf4j
@Component
public class jsonUtils {
    public String basedir = "/BOOT-INF/classes/";
//    public String basedir = "src/main/resources/";

    public String getjson(){
        String fileName = basedir + "other/MoYuData.json";
        if ("/BOOT-INF/classes/".equals(basedir)){
            String contents = "";
            try (InputStream inputStream = getClass().getResourceAsStream(fileName);
                 BufferedReader reader0 = new BufferedReader(new InputStreamReader(inputStream,"UTF8"))) {
                contents = reader0.lines()
                        .collect(Collectors.joining(System.lineSeparator()));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return contents;
        }else{
            /**
             * 以下为idea获取，以上为打包后
             */
            String jsonStr = "";
            try {
                File jsonFile = new File(fileName);
                FileReader fileReader = new FileReader(jsonFile);
                Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
                int ch = 0;
                StringBuffer sb = new StringBuffer();
                while ((ch = reader.read()) != -1) {
                    sb.append((char) ch);
                }
                fileReader.close();
                reader.close();
                jsonStr = sb.toString();
                return jsonStr;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    }
}
